package com.auto.autoapp.task

import android.util.Log
import android.view.accessibility.AccessibilityNodeInfo
import com.auto.autoapp.constants.Dy
import com.auto.autoapp.model.CommentInfo
import com.auto.autoapp.model.Direction
import com.auto.autoapp.step.clickCommentBtn
import com.auto.autoapp.step.exchangeVideo
import com.auto.autoapp.step.handleStep
import com.auto.autoapp.step.isLiveVideo
import com.auto.autoapp.step.replyComment
import com.auto.autoapp.step.toDyIndex
import com.auto.autoapp.util.addLog
import com.auto.autoapp.util.clickOrTap
import com.auto.autoapp.util.getNodeById
import com.ven.assists.AssistsCore
import com.ven.assists.AssistsCore.findById
import com.ven.assists.AssistsCore.getBoundsInParent
import com.ven.assists.AssistsCore.getBoundsInScreen
import com.ven.assists.stepper.Step
import com.ven.assists.stepper.StepCollector
import com.ven.assists.stepper.StepImpl
import kotlinx.coroutines.delay
import kotlin.random.Random

/**
 * 处理评论列表
 * 1:满足条件的点赞
 * 2.满足条件的回复
 * 3.获取满足条件的评论列表
 * //假设需要处理20条评论
 */
class HandleCommentTask : StepImpl() {
    val commentList = mutableListOf<CommentInfo>()
    val maxCount = 5
    override fun onImpl(collector: StepCollector) {
        collector.next(1) {
            return@next toDyIndex()
        }.next(2) {
            commentList.clear()
            val isLive = isLiveVideo()
            if (isLive.isSuccess) {
                return@next Step.get(3)
            }
            var click = clickCommentBtn()
            if (click.isSuccess) {
                delay(1000)
            }
            return@next Step.get(3)

        }.next(3) {
            var bottomRv = Dy.ID_COMMENT_RV.getNodeById()
            if (bottomRv.isFailure) {
                AssistsCore.back()
                return@next Step.get(4)
            }
            val bottomRvNode = bottomRv.getOrThrow()
            val bounds = bottomRvNode.getBoundsInScreen()
            val addableChildren = mutableListOf<CommentInfo>()
            var handleNode: AccessibilityNodeInfo? = null
            val bottomItems = bottomRvNode.findById(Dy.ID_COMMENT_ITEM)
            for (i in 0 until bottomItems.size) {
                var item = bottomItems.get(index = i)
                val isFullVisible =
                    item.findById(Dy.ID_REPLY_COMMENT).firstOrNull()?.isVisibleToUser == true
                if (isFullVisible) {
                    val commentInfo = CommentInfo(
                        nickName = item.findById(Dy.ID_COMMENT_TITLE)
                            .firstOrNull()?.text?.toString(),
                        content = item.findById(Dy.ID_COMMENT_CONTENT)
                            .firstOrNull()?.text?.toString(),
                    )
                    if (!commentList.contains(commentInfo)) {
                        handleNode = item
                        addableChildren.add(commentInfo)
                        break
                    }
                }
            }

            if (commentList.size >= maxCount || addableChildren.isEmpty()) {
                AssistsCore.back()
                return@next Step.get(4)
            }
            commentList.add(addableChildren[0])
//            if (Random.nextInt(1, 10) > 7) {
//                val likeNode = handleNode.findById(Dy.ID_COMMENT_LIKE).firstOrNull()
//                likeNode?.clickOrTap()
//                delay(500)
//            }
//            if (Random.nextInt(1, 10) > 7) {
//                val replayResult = replyComment("1")
//                delay(500)
//            }


            addLog("rv:${bounds}--${bottomRvNode.getBoundsInScreen()}")
            addLog("addable:${handleNode?.getBoundsInParent()}")
            val centerX = bounds.centerX().toFloat()

            val startLocation = floatArrayOf(centerX, bounds.centerY().toFloat())
            val endLocation = floatArrayOf(
                centerX,
                bounds.centerY().toFloat() - handleNode?.getBoundsInScreen()!!.height()
            )

            val result =
                AssistsCore.gesture(
                    startLocation,
                    endLocation,
                    100,
                    300
                )
            if (result) {
                addLog("滑动评论成功")
                delay(500)
            }

            return@next Step.repeat
        }.next(4) {
            var exchangeResult = exchangeVideo()
            if (exchangeResult.isFailure) {
                return@next Step.none
            }
            val isLive = isLiveVideo()
            if (isLive.isSuccess) {
                return@next Step.repeat
            }

            return@next Step.get(2)
        }
    }
}